package com.source.lightfowl.basedata.dao;

import com.source.lightfowl.basedata.entity.IdCard;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 12:51
 */
public interface IdCardDao extends JpaRepository<IdCard,Integer>, JpaSpecificationExecutor<IdCard> {
    @Modifying
    @Transactional
    @Query(value = "delete from id_card s where s.id in (?1)" , nativeQuery = true)
    void deleteBatch(ArrayList<Integer> ids);

    Optional<IdCard> findByNumber(int cardId);

    @Modifying
    @Transactional
    @Query(value = "update id_card set state = ?1 where `number` = ?2",nativeQuery = true)
    void updateState(int i, String number);

    @Modifying
    @Transactional
    @Query(value = "update id_card set state = '0',userId=?1 where `number` = ?2",nativeQuery = true)
    void updateGrant(String userId, String number);

    @Modifying
    @Transactional
    @Query(value = "update id_card set state = '1',userId=null where `number` = ?",nativeQuery = true)
    void updateRecovery(String number);
}
